﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using aoprinter.Model;

namespace aoprinter.Draw
{
    class ArcLineOper : ILineTypeOper
    {
        const string IS_ARC = "isArc";
        const string ARC_DEFAULT = "arcDefault";
        public GraphicsPath DrawLine(PointF point1, PointF point2, Dictionary<string, string> parameters)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(point1.X, point2.Y, point1.X, point2.Y);
            return path;
        }

        public GraphicsPath DrawRect(RectangleF rect, Dictionary<string, string> parameters)
        {
            GraphicsPath path = new GraphicsPath();
            if (parameters.ContainsKey(IS_ARC))
            {
                string strIsArc = parameters[IS_ARC];
                string[] isArcArray = strIsArc.Split(';');
                float xLengthDef = rect.Width / 10;
                float yLengthDef = rect.Height / 10;
                if (parameters.ContainsKey(ARC_DEFAULT))
                {
                    string[] defaultArray = parameters[ARC_DEFAULT].Split(',');
                    if (defaultArray.Length < 2)
                    {
                        float.TryParse(parameters[ARC_DEFAULT], out xLengthDef);
                        float.TryParse(parameters[ARC_DEFAULT], out yLengthDef);
                    }
                    else
                    {
                        float.TryParse(defaultArray[0], out xLengthDef);
                        float.TryParse(defaultArray[1], out yLengthDef);
                    }
                }
                for (int i = 0; i < isArcArray.Length; i++)
                {

                    string[] arcArraySub = isArcArray[i].Split(',');
                    bool isArc = false;
                    bool.TryParse(arcArraySub[0], out isArc);
                    if (isArc)
                    {
                        float xLength = xLengthDef;
                        float yLength = yLengthDef;
                        if (arcArraySub.Length > 1)
                        {
                            float.TryParse(arcArraySub[1], out xLength);
                        }
                        if (arcArraySub.Length > 2)
                        {
                            float.TryParse(arcArraySub[2], out yLength);
                        }
                        xLength = xLength > 0 ? xLength : xLengthDef;
                        yLength = yLength > 0 ? yLength : yLengthDef;
                        RectangleF arcRect = new RectangleF();
                        switch (i)
                        {
                            case 0:
                                arcRect = new RectangleF(rect.X, rect.Y, xLength, yLength);
                                break;
                            case 1:
                                arcRect = new RectangleF(rect.X + rect.Width - xLength, rect.Y, xLength, yLength);
                                break;
                            case 2:
                                arcRect = new RectangleF(rect.X + rect.Width - xLength, rect.Y + rect.Height - yLength, xLength, yLength);
                                break;
                            case 3:
                                arcRect = new RectangleF(rect.X, rect.Y + rect.Height - yLength, xLength, yLength);
                                break;
                            default:
                                break;
                        }
                        path.AddArc(arcRect, 180 + 90 * i, 90);
                    }
                    else
                    {
                        switch (i)
                        {
                            case 0:
                                path.AddLine(rect.X, rect.Y, rect.X, rect.Y);
                                break;
                            case 1:
                                path.AddLine(rect.X + rect.Width, rect.Y, rect.X + rect.Width, rect.Y);
                                break;
                            case 2:
                                path.AddLine(rect.X + rect.Width, rect.Y + rect.Height, rect.X + rect.Width, rect.Y + rect.Height);
                                break;
                            case 3:
                                path.AddLine(rect.X, rect.Y + rect.Height, rect.X, rect.Y + rect.Height);
                                break;
                            default:
                                break;
                        }
                    }
                }
                path.CloseFigure();
            }
            else
            {
                path.AddRectangle(rect);
            }
            return path;
        }
    }
}
